﻿using UnityEngine;
using System.Collections.Generic;
public static class Collider2DExtensions
{
    public static float GetArea(this Collider2D collider)
    {
        if(collider is BoxCollider2D)
        {
            var siz = (collider as BoxCollider2D).size;
            return siz.x * siz.y;
        }else if(collider is CircleCollider2D)
        {
            var r = (collider as CircleCollider2D).radius;
            return Mathf.PI * r * r;
        }else if(collider is PolygonCollider2D)
        {
;           return (collider as PolygonCollider2D).GetPolygonColliderArea();
        }
        else
        {
            Debug.LogError($"{collider.GetType()}:没有为此类型编写计算面积的函数");
            return 0;
        }

    }
    public static void ChangeLayer(this GameObject item,LayerMask layer)
    {
        item.layer = layer;
        for(int i = 0; i < item.transform.childCount; i++)
        {
            item.transform.GetChild(i).gameObject.ChangeLayer(layer);
        }
    }
  
}